﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Sbook
{
    public partial class NewsDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //if (Request.QueryString["nID"] != null)
                if(RouteData.Values["Id"]!=null)
                {
                    //try
                    //{
                        //string Id = Request.QueryString["nID"];
                        string Id = RouteData.Values["Id"].ToString();
                       
                        BindNews(Id);
                        lblGroup.Text = "Tin " + Business.NewsGroupService.NewsGroup_GetById(Business.NewsService.News_GetById(Id)[0].groupID)[0].GroupName;
                        //ShowPopUpMsg(Business.NewsService.News_GetById(Id)[0].nTitle.ToString().Replace(" ","-"));
                    //}
                    //catch { }
                }
            }
        }

        private void BindNews(string Id)
        {
            try
            {
                List<Data.NewsInfo> list = Business.NewsService.News_GetById(Id);
                dtlNews.DataSource = list;
                dtlNews.DataBind();
                dtlNewsOthers.DataSource = Business.NewsService.News_GetByTop("5", "GroupID =" + list[0].groupID + " and NId <>" + Id, "NDate desc");
                dtlNewsOthers.DataBind();
            }
            catch { }
        }

        private void ShowPopUpMsg(string msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("alert('");
            sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
            sb.Append("');");
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
        }

        public string GetTitleById(object Id)
        {
            string title = Business.NewsService.News_GetById(Id.ToString())[0].nTitle.ToString();
            return title.Replace("\"", "").Replace(" ", "");
        }

        public string GetGroupNameById(object Id)
        {
            string groupName = Business.NewsGroupService.NewsGroup_GetById(Id.ToString())[0].GroupName;
            return groupName.Replace(" ", "");
        }
    }
}